<template>
    <div>
        <Category :scene="scene" />
        <el-card style="margin-top: 0.625rem;">
            <div v-show="scene == 0">
                <el-button type="primary" size="default" icon="Plus" @click="addSpu"
                    :disabled="categoryStore.c3Id ? false : true" v-has="`btn.Spu.add`">添加SPU</el-button>
                <el-table border style="margin: 0.625rem;" :data="records">
                    <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
                    <el-table-column label="SPU名称" width="120px" prop="spuName"></el-table-column>
                    <el-table-column label="SPU描述" prop="description"></el-table-column>
                    <el-table-column label="操作" width="240px">
                        <template #="{ row, $index }">
                            <el-button type="primary" size="small" icon="Plus" title="添加SKU" @click="addSku(row)"
                                v-has="`btn.Spu.addsku`"></el-button>
                            <el-button type="primary" size="small" icon="Edit" title="修改SPU" @click="updateSpu(row)"
                                v-has="`btn.Spu.update`"></el-button>
                            <el-button type="primary" size="small" icon="View" title="查看SKU" @click="findSku(row)"
                                v-has="`btn.Spu.spus`"></el-button>
                            <el-popconfirm :title="`您确定删除${row.spuName}吗？`" width="12rem" icon="Delete"
                                @confirm="removeSpu(row.id)">
                                <template #reference>
                                    <el-button type="primary" size="small" icon="Delete" title="删除SPU"
                                        v-has="`btn.Spu.delete`"></el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页器-->
                <el-pagination :pager-count="9" v-model:current-page="pageNo" v-model:page-size="limit"
                    :page-sizes="[3, 4, 5, 6]" :background="true" layout="prev, pager, next, jumper, ->, sizes, total"
                    :total="total" @current-change="getHasSpu" @size-change="changeSize"
                    :disabled="categoryStore.c3Id ? false : true" />
            </div>
            <spuForm ref="Spu" v-show="scene == 1" @changeScene="changeScene"></spuForm>
            <skuForm ref="Sku" v-show="scene == 2" @changeScene="changeScene"></skuForm>
            <el-dialog v-model="skuShow" title="SKU列表">
                <el-table border :data="skuArr">
                    <el-table-column label="SKU名称" prop="skuName"></el-table-column>
                    <el-table-column label="SKU价格" prop="price"></el-table-column>
                    <el-table-column label="SKU重量" prop="weight"></el-table-column>
                    <el-table-column label="SKU图片">
                        <template #="{ row, $index }">
                            <img :src="row.skuDefaultImg" style="width: 8rem; height: 8rem;">
                        </template>
                    </el-table-column>
                </el-table>
            </el-dialog>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import spuForm from './spuForm.vue'
import skuForm from './skuForm.vue'
import { ref, watch, onBeforeUnmount } from 'vue'
//获取分类的仓库
import useCategoryStore from '@/store/modules/category'
import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu'
import type { HasSpuResponseData, Records, SpuData, SkuInfoData, SkuData } from '@/api/product/spu/type'
import { ElMessage } from 'element-plus'

let categoryStore = useCategoryStore()
//定义card组件内容切换变量，0：展示已有SPU，1：添加或修改已有SPU，2：添加SKU
let scene = ref<number>(0)
//当前页面的页码
let pageNo = ref<number>(1)
//每一页展示的数据项数量
let limit = ref<number>(3)
//存储已有品牌数据的总数
let total = ref<number>(0)
//储存已有的SPU数据
let records = ref<Records>([])
//获取子组件实例SpuForm
let Spu = ref<any>()
//获取子组件实例SkuForm
let Sku = ref<any>()
//存储全部的SKU数据
let skuArr = ref<SkuData[]>([])
//控制SKU列表的显示与隐藏
let skuShow = ref<boolean>(false)

//获取某三级分类下全部的已有SPU
const getHasSpu = async (pager = 1) => {
    //修改当前页码
    pageNo.value = pager
    let result: HasSpuResponseData = await reqHasSpu(pageNo.value, limit.value, categoryStore.c3Id)
    if (result.code == 200) {
        records.value = result.data.records
        total.value = result.data.total
    }
}
//分页器下拉菜单发生变化时触发
const changeSize = () => {
    getHasSpu()
}
//添加SPU按钮的回调
const addSpu = () => {
    scene.value = 1
    Spu.value.initSpu(categoryStore.c3Id)
}
//修改SPU按钮的回调
const updateSpu = (row: SpuData) => {
    scene.value = 1
    //调用子组件实例方法获取完整已有的spu数据
    Spu.value.initHasSpuData(row)
}
//spuForm绑定自定义事件
const changeScene = (obj: any) => {
    scene.value = obj.flag
    //再次获取全部的已有的SPU
    if (obj.params == 'update') {
        getHasSpu(pageNo.value)
    } else {
        getHasSpu()
    }
}
//添加SKU按钮的回调
const addSku = (row: SpuData) => {
    scene.value = 2
    //调用子组件实例方法获取完整已有的spu数据
    Sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
//查看SKU列表的数据
const findSku = async (row: SpuData) => {
    let result: SkuInfoData = await reqSkuList((row.id as number))
    if (result.code == 200) {
        skuArr.value = result.data
        skuShow.value = true
    }
}
//删除已有SPU按钮的回调
const removeSpu = async (spuId: number) => {
    let result: any = await reqRemoveSpu(spuId)
    if (result.code == 200) {
        ElMessage({
            type: 'success',
            message: '删除成功'
        })
        getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
    } else {
        ElMessage({
            type: 'error',
            message: '删除失败'
        })
    }
}

//监听仓库三级分类id的变化
watch(() => categoryStore.c3Id, () => {
    if (!categoryStore.c3Id) {
        return
    }
    getHasSpu()
})
//路由组件销毁前，清空仓库关于分类的数据
onBeforeUnmount(() => {
    categoryStore.$reset()
})
</script>

<style scoped lang="scss"></style>